/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desktopapplication1;

import java.util.*;

/**
 *
 * @author TEST
 */
public class Game {
    
    private Integer numberOfPlayers;
    private Integer numberOfRounds;
    private Integer numberOfMoves;
    private Integer strategy; //0-wet za wet, 1-zawsze oszukuj, 2-zawsze wspolpracuj
    private Integer playerProfitCC = 20;
    private Integer playerProfitCD = 0;
    private Integer playerProfitDC = 30;
    private Integer playerProfitDD = 10;
    private Random generator = new Random();
    private Map<Integer,Map<String,Double>> playersProbabilities;
    private Map<Integer,String> firstPlayersMoves;
    private Map<Integer,String> secondPlayersMoves;
    private Map<Integer,Integer> firstPlayersPoints;
    
    
    public Game(Integer newNumberOfPlayers, Integer newNumberOfRounds, Integer newNumberOfMoves, Integer newStrategy) {
        numberOfPlayers = newNumberOfPlayers;
        numberOfRounds = newNumberOfRounds;
        numberOfMoves = newNumberOfMoves;
        strategy = newStrategy;
        playersProbabilities = new HashMap<Integer,Map<String,Double>>();
        MapOfProbabilities mapOfProbabilities;
        for(int i = 0; i < numberOfPlayers; i++) {
            mapOfProbabilities = new MapOfProbabilities(newNumberOfMoves);
            Map<String,Double> probabilities = mapOfProbabilities.getMapOfProbabilities();
            
            playersProbabilities.put(i, probabilities);
        }
        firstPlayersMoves = new HashMap<Integer,String>();
        secondPlayersMoves = new HashMap<Integer,String>();
        firstPlayersPoints = new HashMap<Integer,Integer>();
    }
    
    
    public void setNumberOfPlayers (Integer newNumberOfPlayers) {
        numberOfPlayers = newNumberOfPlayers;
    }
    
    public Integer getNumberOfPlayers () {
        return numberOfPlayers;
    }
    
    public void setNumberOfRounds (Integer newNumberOfRounds) {
        numberOfRounds = newNumberOfRounds;
    }
    
    public Integer getNumberOfRounds () {
        return numberOfRounds;
    }
    
    public void setNumberOfMoves (Integer newNumberOfMoves) {
        numberOfMoves = newNumberOfMoves;
    }
    
    public Integer getNumberOfMoves () {
        return numberOfMoves;
    }
    
    public void setStrategy (Integer newStrategy) {
        strategy = newStrategy;
    }
    
    public Integer getStrategy () {
        return strategy;
    }
    
    public Map<Integer,Map<String,Double>> getPlayersProbabilities () {
        return playersProbabilities;
    }
    
    public Map<Integer,String> getFirstPlayersMoves () {
        return firstPlayersMoves;
    }
    
    public Map<Integer,String> getSecondPlayersMoves () {
        return secondPlayersMoves;
    }
    
    public Map<Integer,Integer> getFirstPlayersPoints () {
        return firstPlayersPoints;
    }
    
    public String getRandomStrategy () {
        String alphabet = "CD";
        char strategy = alphabet.charAt(generator.nextInt(alphabet.length()));
        return Character.toString(strategy);
    }
    
    public String getPlayerMoves (List<String> moves) {
        StringBuilder stringBuilder = new StringBuilder();
        for(String s : moves) {
            stringBuilder.append(s);
        }
        return stringBuilder.toString();
    }
    
    
    public void startGame() {
        List<String> listOfFirstPlayerMoves;
        List<String> listOfSecondPlayerMoves;
        String firstPlayerMoves;
        String secondPlayerMoves;        
        String secondPlayerLastMoves;
        String firstPlayerMove;
        String secondPlayerMove;
        Integer firstPlayerPoints;  
        
        for(int playerNumber = 0; playerNumber < numberOfPlayers; playerNumber++) {  
            listOfFirstPlayerMoves = new ArrayList<String>();
            listOfSecondPlayerMoves = new ArrayList<String>();
            firstPlayerMoves = null;
            secondPlayerMoves = null;        
            secondPlayerLastMoves = null;
            firstPlayerMove = null;
            secondPlayerMove = null; 
            firstPlayerPoints = 0;
            int i;
            for(i = 0; i < numberOfMoves; i++) {
                firstPlayerMoves = getPlayerMoves(listOfFirstPlayerMoves); 

                firstPlayerMove = getRandomStrategy();
                if(strategy == 0) {
                    if(i == 0) secondPlayerMove = "C";
                    else secondPlayerMove = Character.toString(firstPlayerMoves.charAt(i-1));
                }
                else if(strategy == 1) secondPlayerMove = "D"; 
                else secondPlayerMove = "C";

                listOfFirstPlayerMoves.add(firstPlayerMove);
                listOfSecondPlayerMoves.add(secondPlayerMove);

                if(firstPlayerMove.equals("C") && secondPlayerMove.equals("C")) firstPlayerPoints += playerProfitCC;
                else if(firstPlayerMove.equals("C") && secondPlayerMove.equals("D")) firstPlayerPoints += playerProfitCD;
                else if(firstPlayerMove.equals("D") && secondPlayerMove.equals("C")) firstPlayerPoints += playerProfitDC;
                else firstPlayerPoints += playerProfitDD;
            }        

            for(; i < numberOfRounds; i++) {   
                firstPlayerMoves = getPlayerMoves(listOfFirstPlayerMoves);            
                secondPlayerMoves = getPlayerMoves(listOfSecondPlayerMoves);            
                secondPlayerLastMoves = secondPlayerMoves.substring(secondPlayerMoves.length() - numberOfMoves);

                if(playersProbabilities.get(playerNumber).get(secondPlayerLastMoves) > 0.5) firstPlayerMove = "C";
                else firstPlayerMove = "D";

                if(strategy == 0) {
                    if(i == 0) secondPlayerMove = "C";
                    else secondPlayerMove = Character.toString(firstPlayerMoves.charAt(i-1));
                }
                else if(strategy == 1) secondPlayerMove = "D"; 
                else secondPlayerMove = "C";

                listOfFirstPlayerMoves.add(firstPlayerMove);
                listOfSecondPlayerMoves.add(secondPlayerMove);

                if(firstPlayerMove.equals("C") && secondPlayerMove.equals("C")) firstPlayerPoints += playerProfitCC;
                else if(firstPlayerMove.equals("C") && secondPlayerMove.equals("D")) firstPlayerPoints += playerProfitCD;
                else if(firstPlayerMove.equals("D") && secondPlayerMove.equals("C")) firstPlayerPoints += playerProfitDC;
                else firstPlayerPoints += playerProfitDD;
            }

            firstPlayersMoves.put(playerNumber, getPlayerMoves(listOfFirstPlayerMoves));
            secondPlayersMoves.put(playerNumber, getPlayerMoves(listOfSecondPlayerMoves));
            firstPlayersPoints.put(playerNumber, firstPlayerPoints);
        }
    }
    
    
}
